/**
 * Created by Admin on 2018/1/4.
 */
define(function (require, exports, module) {
    require("../seajs-init");
    var Statistic = require("./statistic.js");
    var tpl = require("./tpl/vaildIPatTpl.html");
    var uri = require("uri");
    var wlc = window.location;
    var uriParam = uri.getParams(wlc.href, true);

    var statisticUrl = "";
    var exportUrl = "", linetitleD;
    switch ($(".statistical-title").html()) {
        case "有效发明":
            statisticUrl = 'statistic/validipat';
            exportUrl = 'excel/validipat';
            linetitleD = "统计截止到某月,各区域的已授权且目前仍处于有效状态的发明专利总量。";
            tpl = require('./tpl/yxfmVaildIPatTpl.html');
            yxfmRedirct();
            break;
        case "万人有效发明专利拥有量":
            statisticUrl = 'statistic/millionipat';
            exportUrl = 'excel/millionipat';
            linetitleD = "统计截止到某月，各区域每万人拥有的经知识产权行政部门授权且在有效期内的发明专利总量。";
            tpl = require('./tpl/yxfmVaildIPatTpl.html');
            yxfmRedirct();
            break;
        case "专利申请":
            statisticUrl = 'statistic/patapply';
            exportUrl = 'excel/patapply';
            linetitleD = "用户自行导入的今年各个月份的申请数据。";
            tpl = require('./tpl/yxfmVaildIPatTpl.html');
            zlsqRedirct();
            break;
        case "发明专利申请":
            statisticUrl = 'statistic/ipatapply';
            exportUrl = 'excel/ipatapply';
            linetitleD = "用户自行导入的今年各个月份的发明申请数据。";
            tpl = require('./tpl/yxfmVaildIPatTpl.html');
            fmzlRedirct();
            break;
        case "专利授权":
            statisticUrl = 'statistic/patauthorize';
            exportUrl = 'excel/patauthorize';
            linetitleD = "统计从本年1月起，截止到某月，各区域经国家知识产权局公开的新增授权的专利总量。";
            tpl = require('./tpl/yxfmVaildIPatTpl.html');
            zlApproveRedirct();
            break;
        case "万人专利授权指数":
            statisticUrl = 'statistic/wrpatauthorize';
            exportUrl = 'excel/wrpatauthorize';
            linetitleD = "统计从本年1月起，截止到某月，各区域每万人拥有的国家知识产权局公布的新增专利授权总量。";
            tpl = require('./tpl/yxfmVaildIPatTpl.html');
            zlApproveRedirct();
            break;
        case "发明专利授权":
            statisticUrl = 'statistic/ipatauthorize';
            exportUrl = 'excel/ipatauthorize';
            linetitleD = "统计从本年1月起，截止到某月，各区域经国家知识产权局公开的新增授权的发明专利总量。";
            tpl = require('./tpl/yxfmVaildIPatTpl.html');
            fmzlsqRedirct();
            break;
        case "万人发明专利授权指数":
            statisticUrl = 'statistic/wripatauthorize';
            exportUrl = 'excel/wripatauthorize';
            linetitleD = "统计从本年1月起，截止到某月，各区域每万人拥有的国家知识产权局公布的新增发明专利授权总量。";
            tpl = require('./tpl/yxfmVaildIPatTpl.html');
            fmzlsqRedirct();
            break;
        case "有效专利":
            statisticUrl = 'statistic/yxpatent';
            exportUrl = 'excel/yxpatent';
            linetitleD = "统计截止到某月,各区域的已授权且目前仍处于有效状态的专利总量。";
            break;
        case "万人有效专利拥有量":
            statisticUrl = 'statistic/wryxpatent';
            exportUrl = 'excel/wryxpatent';
            linetitleD = "统计截止到某月,各区域每万人拥有的经知识产权行政部门授权且在有效期内的发明专利总量。";
            break;
        case "实质审查":
            statisticUrl = 'statistic/szsc';
            exportUrl = 'excel/szsc';
            linetitleD = "统计截止到某月，各区域处于实质审查阶段的发明专利总量。";
            break;
    }
    Statistic.initTable(window.jkconfig.webapiurlv + statisticUrl, window.jkconfig.webapiurlv + exportUrl, uriParam.year == "0" ? 0 : 1, tpl, "#Js_statisticalBox", $(".statistical-title").html(), linetitleD);
    $(function () {
        if (uriParam.year == "0") {
            $(".time-nav").children().eq(1).addClass("time-selct");
        } else {
            $(".time-nav").children().eq(0).addClass("time-selct");
        }
    })


    /**
     * 有效发明-跳转事件
     */
    function yxfmRedirct() {
        $('#Js_statisticalBox').on('click', 'td', function () {
            var $self = $(this)
            var $parent = $self.parent('tr')
            var areaId = $parent.children('td:eq(1)').attr('data-area-id')
            if (areaId) {
                localStorage.setItem("areaId", areaId);
            }

            localStorage.setItem("patHubTypeName", JSON.stringify(['发明公开', '发明授权']));
            localStorage.setItem("lawState", JSON.stringify([1]));
            window.top.location.href = "/index.html#/analysis/patSearch.html";
        })
    }

    // 专利申请跳转
    function zlsqRedirct() {
        $('#Js_statisticalBox').on('click', 'td', function () {
            var $self = $(this)
            var $parent = $self.parent('tr')
            var areaId = $parent.children('td:eq(1)').attr('data-area-id')
            if (areaId) {
                localStorage.setItem("areaId", areaId);
            }

            var year = new Date().getFullYear()
            if (uriParam.year == "0") {
                year = new Date().getFullYear() - 1
            }
            debugger
            var month = PrefixInteger(($self.index() - 1), 2).toString()
            localStorage.setItem("patApplyDateBegin", year.toString() + '-' + '01' + '-' + '01');
            localStorage.setItem("patApplyDateEnd", year.toString() + '-' + month + '-' + mGetDate(year, $self.index() - 1));
            window.top.location.href = "/index.html#/analysis/patSearch.html";
        })
    }

    // 专利授权跳转
    function zlApproveRedirct() {
        $('#Js_statisticalBox').on('click', 'td', function () {
            var $self = $(this)
            var $parent = $self.parent('tr')
            var areaId = $parent.children('td:eq(1)').attr('data-area-id')
            if (areaId) {
                localStorage.setItem("areaId", areaId);
            }

            var year = new Date().getFullYear()
            if (uriParam.year == "0") {
                year = new Date().getFullYear() - 1
            }
            var month = PrefixInteger(($self.index() - 1), 2).toString()
            localStorage.setItem("patAppDateBegin", year.toString() + '-' + '01' + '-' + '01');
            localStorage.setItem("patAppDateEnd", year.toString() + '-' + month + '-' + mGetDate(year, $self.index() - 1));
            window.top.location.href = "/index.html#/analysis/patSearch.html";
        })
    }


    // 发明专利申请
    function fmzlRedirct() {
        $('#Js_statisticalBox').on('click', 'td', function () {
            var $self = $(this)
            var $parent = $self.parent('tr')
            var areaId = $parent.children('td:eq(1)').attr('data-area-id')
            if (areaId) {
                localStorage.setItem("areaId", areaId);
            }
            var year = new Date().getFullYear()
            if (uriParam.year == "0") {
                year = new Date().getFullYear() - 1
            }
            var month = PrefixInteger(($self.index() - 1), 2).toString()
            localStorage.setItem("patApplyDateBegin", year.toString() + '-' + '01' + '-' + '01');
            localStorage.setItem("patApplyDateEnd", year.toString() + '-' + month + '-' + mGetDate(year, $self.index() - 1));
            localStorage.setItem("patHubTypeName", JSON.stringify(['发明公开', '发明授权']));
            window.top.location.href = "/index.html#/analysis/patSearch.html";
        })
    }

    // 发明专利授权
    function fmzlsqRedirct() {
        $('#Js_statisticalBox').on('click', 'td', function () {
            var $self = $(this)
            var $parent = $self.parent('tr')
            var areaId = $parent.children('td:eq(1)').attr('data-area-id')
            if (areaId) {
                localStorage.setItem("areaId", areaId);
            }
            var year = new Date().getFullYear()
            if (uriParam.year == "0") {
                year = new Date().getFullYear() - 1
            }
            var month = PrefixInteger(($self.index() - 1), 2).toString()
            localStorage.setItem("patAppDateBegin", year.toString() + '-' + '01' + '-' + '01');
            localStorage.setItem("patAppDateEnd", year.toString() + '-' + month + '-' + mGetDate(year, $self.index() - 1));
            localStorage.setItem("patHubTypeName", JSON.stringify(['发明公开', '发明授权']));
            window.top.location.href = "/index.html#/analysis/patSearch.html";
        })
    }


    // 获取一个月中的最大天数
    function mGetDate(year, month) {
        var d = new Date(year, month, 0);
        return d.getDate();
    }

    // js 补0
    function PrefixInteger(num, n) {
        return (Array(n).join(0) + num).slice(-n);
    }


});